package seotools.positions;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ExcelPrice {
    public static void main(String[] args) throws IOException {
//         FileInputStream is = new FileInputStream("price.xml");
        Document doc = Jsoup.parse(new File("price.xml"), "UTF-8");
//        System.out.println(doc);
//        Elements rows = doc.select("cell[ss:styleid=s36] data");
        PrintStream out = new PrintStream(new FileOutputStream("items-import.txt"));
        Element table = doc.select("table").get(0);
        Elements rows = table.children();
        Category currCat = null;
        List<Item> items = new ArrayList<Item>();
        for (int i = 0; i < rows.size(); i ++) {
            if (rows.get(i).children().size() == 0) {
                continue;
            }
            String attr = rows.get(i).child(0).attr("ss:StyleID");
            String name = rows.get(i).child(0).text().replace("/", "\\");
            if (attr.equals("s26")) {
                Category cat = new Category();
                cat.name = name;
                cat.level = 0;
                cat.parent = null;
                currCat = cat;
            } else if (attr.equals("s30")) {
                while (currCat.level != 0) {
                    currCat = currCat.parent;
                }
                Category cat = new Category();
                cat.name = name;
                cat.parent = currCat;
                cat.level = 1;
                currCat = cat;
            } else if (attr.equals("s33")) {
                while (currCat.level != 1) {
                    currCat = currCat.parent;
                }
                Category cat = new Category();
                cat.name = name;
                cat.level = 2;
                cat.parent = currCat;
                currCat = cat;
            } else if (attr.equals("s36")) {
                Item item = new Item();
                item.name = name;
                item.desc = "добавить описание для товара: " + name;
                item.count = rows.get(i).child(1).text();
                item.code = rows.get(i).child(2).text();
                item.priceHigh = rows.get(i).child(3).text();
                item.priceMedium = rows.get(i).child(5).text();
                item.priceSmall = rows.get(i).child(7).text();
                item.publish = "Y";
                item.category = currCat;
                items.add(item);
            }
        }
        System.out.println(items.size());
        for (Item item : items) {
            System.out.println(item);
            out.println(item);
        }

//        for (Element row : rows) {
//            row.
//            System.out.println(row.text());
//        }
//        FileInputStream is = new FileInputStream("price.xls");
//        POIFSFileSystem fs = new POIFSFileSystem(is);
//        POIOLE2TextExtractor extractor = ExtractorFactory.createExtractor(fs);
//
//        String text = extractor.getText();
//        System.out.println("text = " + text);
/*
        BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
        PrintStream out = new PrintStream(new FileOutputStream("rows-import.txt"));

        String line = null;
        Category currCat = null;
        boolean wasCat = false;
        List<Item> rows = new ArrayList<Item>();
        while ((line = reader.readLine()) != null) {
            String[] fields = line.split("\t");
            if (fields.length == 1) { // category
                Category cat = new Category();
                cat.name = fields[0];
                if (wasCat) {
                    cat.parent = currCat;
                }
                currCat = cat;
                wasCat = true;
            } else { //item
                Item item = new Item();
                item.name = fields[0];
                item.desc = "добавить описание для товара: " + fields[0];
                item.adCount = fields[1];
                item.code = fields[2];
                item.priceHigh = fields[3];
                item.priceMedium = fields[5];
                item.priceSmall = fields[7];
                item.category = currCat;
                rows.add(item);
                wasCat = false;
            }
        }
        System.out.println(rows.size());
        for (Item item : rows) {
            System.out.println(item);
            out.println(item);
        }
*/
    }

    static class Category {
        String name;
        int level;
        Category parent;

        @Override
        public String toString() {
            if (parent != null) {
                return parent + "/" + name;
            } else {
                return name;
            }
        }
    }

    static class Item {
        String name;
        String desc;
        String count;
        String code;
        String priceSmall;
        String priceMedium;
        String priceHigh;
        String publish;
        Category category;

        @Override
        public String toString() {
            return category + ";" + name + ";" + desc + ";" + count + ";" + code + ";" + priceSmall + ";" + publish;
        }
    }
}
